package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dto.WagesDto;
import com.example.entity.*;
import com.example.mapper.WagesMapper;
import com.example.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class WagesServiceImpl extends ServiceImpl<WagesMapper, Wages> implements WagesService {

    @Autowired
    private PositionService positionService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserWagesService ewService;

    @Autowired
    private UserAService eaService;


    @Override
    public void updateByIdWithUW(WagesDto wagesDto) {

        Integer money = Integer.valueOf(wagesDto.getPositionWages()) + Integer.valueOf(wagesDto.getPerformance()) + Integer.valueOf(wagesDto.getSubsidy());

        wagesDto.setSalary(String.valueOf(money));

        this.updateById(wagesDto);
    }

    @Override
    public void saveWithUW(WagesDto wagesDto) {

        this.save(wagesDto);
        log.info("{}", wagesDto);

        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getId, wagesDto.getUserId());

        User one = userService.getOne(userLambdaQueryWrapper);

        LambdaQueryWrapper<UserA> employeeALambdaQueryWrapper = new LambdaQueryWrapper<>();
        employeeALambdaQueryWrapper.eq(UserA::getUserId, one.getId());
        UserA employeeA = eaService.getOne(employeeALambdaQueryWrapper);

        LambdaQueryWrapper<Position> positionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        positionLambdaQueryWrapper.eq(Position::getId, employeeA.getPositionId());

        wagesDto.setPositionId(positionService.getOne(positionLambdaQueryWrapper).getId());
        wagesDto.setUserName(one.getUsername());
        wagesDto.setPositionWages(positionService.getOne(positionLambdaQueryWrapper).getWages());

        UserWages employeeWages = new UserWages();
        BeanUtils.copyProperties(wagesDto, employeeWages);

        long id = IdWorker.getId();
        employeeWages.setId(id);


        employeeWages.setWagesId(wagesDto.getId());
        log.info("{}", wagesDto);
        log.info("{}", employeeWages);

        Integer money = Integer.valueOf(wagesDto.getPositionWages()) + Integer.valueOf(wagesDto.getPerformance()) + Integer.valueOf(wagesDto.getSubsidy());

        wagesDto.setSalary(String.valueOf(money));
        this.updateById(wagesDto);

        ewService.save(employeeWages);
    }

    @Override
    public WagesDto getByIdWithUW(Long id) {

        Wages wages = this.getById(id);

        WagesDto wagesDto = new WagesDto();
        BeanUtils.copyProperties(wages, wagesDto);

        LambdaQueryWrapper<UserWages> employeeWagesLambdaQueryWrapper = new LambdaQueryWrapper<>();
        employeeWagesLambdaQueryWrapper.eq(UserWages::getWagesId, id);
        wagesDto.setPositionId(ewService.getOne(employeeWagesLambdaQueryWrapper).getPositionId());


        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getId, wagesDto.getUserId());
        LambdaQueryWrapper<Position> positionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        positionLambdaQueryWrapper.eq(Position::getId, wagesDto.getPositionId());

        wagesDto.setUserName(userService.getOne(userLambdaQueryWrapper).getUsername());
        wagesDto.setPositionWages(positionService.getOne(positionLambdaQueryWrapper).getWages());


        return wagesDto;
    }

    @Override
    public void removeByIdWithUW(Long id) {
        this.removeById(id);

        LambdaQueryWrapper<UserWages> employeeWagesLambdaQueryWrapper = new LambdaQueryWrapper<>();
        employeeWagesLambdaQueryWrapper.eq(UserWages::getWagesId, id);

        ewService.removeById(ewService.getOne(employeeWagesLambdaQueryWrapper).getId());
    }
}
